#include "checkdialog.h"
#include "ui_checkdialog.h"
#include "lcs.h"

#include <iostream>
#include <QStandardItemModel>

CheckDialog::CheckDialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::CheckDialog)
{
    m_ui->setupUi(this);
}

CheckDialog::~CheckDialog()
{
    delete m_ui;
}

void CheckDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void CheckDialog::setTranscript(const QStringList &transcripts)
{
    this->m_transcripts = transcripts;
}

void CheckDialog::setAnswer(const QStringList &answers)
{
    this->m_answers = answers;
}

void CheckDialog::performChecking()
{
    QString str1;
    QString str2;

    foreach (QString s, m_transcripts) {
        str1.append(s);
        str1.append("\n");
    }
    foreach (QString s, m_answers) {
        str2.append(s);
        str2.append("\n");
    }

    QString result = LCS(str1, str2);
    result.replace("\n", "<br />");

    m_ui->scriptTextEdit->setText(str1);
    m_ui->answerTextEdit->setHtml(result);

    /*QStandardItemModel *modelTrans = new QStandardItemModel(this);
    QStandardItemModel *modelAns = new QStandardItemModel(this);

    QStringList::const_iterator iterTrans = m_transcripts.begin();
    QStringList::const_iterator iterAns = m_answers.begin();

    int countTrans = 0, countAns = 0;

    while (iterTrans != m_transcripts.end() || iterAns != m_answers.end()) {

        std::cout << "In checking while.\n";

        if (iterTrans != m_transcripts.end()) {
            QStandardItem *itemTrans = new QStandardItem(QString(*iterTrans));
            modelTrans->setItem(countTrans, itemTrans);
        }

        if (iterAns != m_answers.end()) {
            QStandardItem *itemAns = new QStandardItem(QString(*iterAns));

            if (iterTrans != m_transcripts.end()) {
                if ((*iterTrans) == (*iterAns)) {
                    itemAns->setBackground(QBrush(QColor(222, 255, 222)));
                }
                else {
                    itemAns->setBackground(QBrush(QColor(255, 222, 222)));
                }
            }
            else {
                itemAns->setBackground(QBrush(QColor(255, 222, 222)));
            }

            modelAns->setItem(countAns, itemAns);
        }

        if (iterTrans != m_transcripts.end()) {
            iterTrans++;
            countTrans++;
        }
        if (iterAns != m_answers.end()) {
            iterAns++;
            countAns++;
        }
    }

    std::cout.flush();

    QItemSelectionModel *oldsmodel = m_ui->scriptListView->selectionModel();
    m_ui->scriptListView->setModel(modelTrans);
    m_ui->scriptListView->setModelColumn(0);
    delete oldsmodel;

    QItemSelectionModel *oldwmodel = m_ui->writeListView->selectionModel();
    m_ui->writeListView->setModel(modelAns);
    m_ui->writeListView->setModelColumn(0);
    delete oldwmodel;*/
}
